package com.service.impl;

import com.constant.Constant;
import com.dao.CommentDao;
import com.dao.PraiseDao;
import com.dao.UserDao;
import com.entity.Comment;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CommentService;
import com.util.AjaxResult;
import com.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private PraiseDao praiseDao;

    @Override
    public AjaxResult findCommentByResourceId(Integer resourceId, Integer pageNo) {
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        AjaxResult ajaxResult = new AjaxResult(true, "查询成功", null);
        try {
            List<CommentVo> commentVos = commentDao.selectCommentByResourceId(resourceId);
            //遍历commentVos 将nickName和praiseCount属性查出并赋给commentVos对应的每一个评论对象
            for (CommentVo comment : commentVos) {
                Integer user_id = comment.getUser_id();
                User user = userDao.selectUserById(user_id);
//           遍历集合,将nickName和praiseCount属性查出并赋给commentVos对应的每一个评论对象
                comment.setNickname(user.getNickname());
                comment.setPraise_count(praiseDao.selectPraiseCountByCommentId(comment.getId()));
            }
            PageInfo<CommentVo> info = new PageInfo<>(commentVos);
            ajaxResult.setObj(info);
            if (commentVos == null) {
                throw new ServiceException("没有更多评论了！");
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg(e.getMessage());
        }
        return ajaxResult;
    }

    @Override
    public Integer findPraiseCountByCommentId(Integer commentId) {
        Integer i = praiseDao.selectPraiseCountByCommentId(commentId);
        return i;
    }

    @Override//context,create_date,user_id,resource_id,status
    public AjaxResult addComment(String context, Integer resourceId, Integer userId) {
        AjaxResult ajaxResult = new AjaxResult();
        Comment comment = new Comment(context,new Date(),userId,resourceId,2);
        try {
            Integer i = commentDao.insertComment(comment);
            ajaxResult.setMsg("发布评论成功！");
        } catch (Exception e) {
            ajaxResult.setMsg("评论失败");
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @Transactional
    @Override
    public int modifyStatusByCommentId(Integer commentId, Integer status) {
        int i = commentDao.updateStatusByCommentId(commentId, status);
        return i;
    }

    @Override
    public PageInfo<CommentVo> findCommentByStatusAndCondition(CommentVo commentVo) {
        Integer pageNo = commentVo.getPageNo();
        Date begin_date = commentVo.getBegin_date();
        Date end_date = commentVo.getEnd_date();
        String context = commentVo.getContext();
        Integer status = commentVo.getStatus();
        PageHelper.startPage(pageNo, Constant.PAGE_SIZE);
        List<CommentVo> commentVos = commentDao.selectCommentByCommentVo(begin_date, end_date, context, status);
        for (CommentVo comment : commentVos) {
            Integer user_id = comment.getUser_id();
            User user = userDao.selectUserById(user_id);
//           遍历集合,将nickName和praiseCount属性查出并赋给commentVos对应的每一个评论对象
            comment.setNickname(user.getNickname());
            comment.setPraise_count(praiseDao.selectPraiseCountByCommentId(comment.getId()));
        }

        return new PageInfo<CommentVo>(commentVos);
    }
}
